package com.psychologyCommunity.Community.service;

import com.psychologyCommunity.Community.dao.MessageMapper;
import com.psychologyCommunity.Community.entity.Message;
import com.psychologyCommunity.Community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class MessageService {
    @Autowired
    private MessageMapper messageMapper;


    public List<Message> findConversations(int userId,int offset,int limit){
        return messageMapper.selectConversions(userId,offset,limit);
    }

    public int findConversationCount(int userId){
        return  messageMapper.selectConversionCount(userId);
    }
    public List<Message> findLetters(String conversationId,int offset,int limit) {
        return messageMapper.selectLetters(conversationId,offset,limit);
}

    public int findLetterCount(String conversationId){
        return  messageMapper.selectCount(conversationId);
    }
    public  int findLetterUnreadCount(int userId,String conversationId){
        return messageMapper.selectLetterUnreadCount(userId,conversationId);
    }
    //添加消息的时候也要注意敏感词的过滤
    @Autowired
    private SensitiveFilter sensitiveFilter;
    public int addMessage(Message message){
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));//先过滤标签
        message.setContent(sensitiveFilter.filter(message.getContent()));//再过滤敏感词
        return messageMapper.insertMessage(message);
    }
    public int  readMessage(List<Integer> ids){
        return messageMapper.updateStatus(ids,1);//更新为已读 1
    }

    public Message findLatestNotice(int userId,String topic){
        return messageMapper.selectLatestNotice(userId,topic);
    }
    public int findNoticeCount(int userId,String topic){
        return messageMapper.selectNoticeCount(userId,topic);
    }
    public int findUnreadCount(int userId,String topic){
        return  messageMapper.selectNoticeUnreadCount(userId,topic);
    }
    public List<Message> findNotices(int userId,String topic,int offset,int limit){
        return messageMapper.selectNotices(userId,topic,offset,limit);
    }
}
